<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>canvas</title>
    <style>
      #canvas {
        display: block;
        margin: 0 auto;
        border: 1px solid #eee;
      }
    </style>
  </head>
  <body>
    <canvas id="canvas" width="400" height="300"></canvas>
    <script>
      /** @type {HTMLCanvasElement} */
      let canvas = document.querySelector("#canvas");
      let ctx = canvas.getContext("2d");
      let pointX = ctx.canvas.width / 2;
      let pointY = ctx.canvas.height / 2;
      ctx.fillRect(20, 20, 40, 40);
      //   1.保存初始状态
      ctx.save();
      ctx.translate(200, 150);
      ctx.beginPath();
      ctx.rotate((45 * Math.PI) / 180);
      ctx.fillRect(-25, -40, 50, 80);
      //   2.恢复保存的状态
      ctx.restore();
      //
      ctx.fillRect(100, 100, 40, 40);
    </script>
  </body>
</html>
